export class AddRightStairs{
    constructor(faSelector) {
        this.father = $(faSelector);
        this.init();
    }
    init(){
        let offset = this.father.offset()
        let right_position = offset.left + 1120;
        $(".rigthFixed").css({
            "position": "fixed",
            "top": offset.top+"px",
            "left": right_position + "px",
            "display":"block"
        })
        this.bindEvent();
    }
    bindEvent(){
        let _this = this
       $("a").on("click",function(e){
           e.preventDefault;
       });
       $(".toShopCar").on("click",function(e){
            e.preventDefault;
            _this.checkLogin();
       })
       $("#toTop").on("click", function () {
            _this.toTop();
       })
       
        $(window).on("scroll",function () {
            let time;
            _this.scrollTop(time);
        });
    }
    checkLogin(){
        let TOKEN = this.cookie("TOKEN");
        if(TOKEN === null || TOKEN===undefined || TOKEN===""){
            console.log(11);
            $("#login").trigger("click");
        }else{
           location.href="../../dist/shopCar.html";
        }
    }
    toTop(){
        $("html,body").stop().animate({ scrollTop: "10" });
    }
    scrollTop(time){
            let offset = this.father.offset()
            let left_position = offset.left - 120;
            let right_position = offset.left + 1120;

            // console.log(left_position);

            clearTimeout(time)
            time = setTimeout(function () {
                let scrolly = $(window).scrollTop();            
                //楼梯吸顶效果
                if (scrolly >= offset.top-100) {
                    $(".rigthFixed").css({
                        "position": "fixed",
                        "top": "100px",
                        "left": right_position + "px"
                    })
                } else {
                    time = setTimeout(function () {
                        $(".rigthFixed").css({
                            "position": "absolute",
                            "top": offset.top+"px",
                            "left": right_position+"px"
                        })
                    }, 0);
                }
            }, 0)
    }
    cookie( name , value , options = {} ){
        // setCookie 
        if( typeof name === "string" && typeof value === "string"){
              // 因为日期对象比较特殊所以我们需要先创建日期对象; 
              // 传入的数据要求是number 类型, 代表的涵义是cookie在几天之后过期; 
              if(typeof options.expires === "number"){
                    var d = new Date();
                    d.setDate( d.getDate() + options.expires );
                    options.expires = d ; 
              }
              // 把所有的参数拼接成符合cookie规则的字符串;
              return document.cookie = [
                    name , "=" , value ,
                    typeof options.path === "string" ? ";path=" + options.path : "",
                    typeof options.domain === "string" ? ";domain=" + options.domain : "" ,
                    typeof options.expires === "object" ? ";expires=" + options.expires : ""
              ].join("");
        }
        // removeCookie 
        if( typeof name === "string" && value === null ){
              return cookie( name , "delete" , Object.assign( {
                    expires : - 1
              }, options ) )
        }
        // getCookie 
        if( typeof name === "string"){
              // 拆分的依据一定是 : 分号*空格*; 一定要记住是有空格的;
              var cookie_array = document.cookie.split("; ");
              // 遍历cookie数组查询正确的cookie数据; 
              for(var i = 0 ; i < cookie_array.length ; i ++){
                    var item = cookie_array[i].split("=")
                    if( item[0] === name ){
                          return item[1];
                    }
              }
              return "";
        }
    }
}



